package com.ruoyi.system.delay;

import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

@Data
public class DelayOrderTask implements Delayed {

    /**
     * 订单号
     */
    private final String orderNo;

    /**
     * 过期时间 毫秒
     */
    private final Long expireTime;

    public DelayOrderTask(String orderNo, Long expireTime) {
        this.orderNo = orderNo;
        this.expireTime = expireTime = System.currentTimeMillis() + expireTime * 60 * 1000;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(expireTime - System.currentTimeMillis(),TimeUnit.NANOSECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        return Long.compare(this.expireTime, ((DelayOrderTask) o).expireTime);
    }
}
